package com.jeff.tianti.cms.dao;

import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeff.tianti.cms.entity.Area;
import com.jeff.tianti.cms.dto.AreaQueryDTO;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;

/**
 * @author xujianfang
 * @desc AreaDaoImpl��
 * @date 2019-07-13
 */

public class AreaDaoImpl extends CustomBaseSqlDaoImpl implements AreaDaoCustom {

    public PageModel<Area> queryAreaPage(AreaQueryDTO areaQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Area t ");
        return this.queryForPageWithParams(hql.toString(), map, areaQueryDTO.getCurrentPage(), areaQueryDTO.getPageSize());
    }

    public List<Area> queryAreaList(AreaQueryDTO areaQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from Area t ");
        if (areaQueryDTO != null) {
            Integer level = areaQueryDTO.getLevel();
            if (level != null) {
                hql.append(" where t.level = "+level);
            }
        }
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public List<Area> findDistrictByAreaId(String areaId) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" select t from Area t where 1 = 1 ");
        if (StringUtils.isNotBlank(areaId)) {
            Integer i = Integer.valueOf(areaId);
            hql.append(" and t.parentAreaId = :areaId ");
            map.put("areaId", i);
        }
        return this.queryByMapParams(hql.toString(), map);
    }

}